package com.sonicvision.resourcetracker.mailmerge;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;

import org.springframework.stereotype.Component;

@Component
public class MailSender {

	public void sendMail(InputStream inputStream, List<Contact> contacts,
			ByteArrayInputStream resumeBAIS, String fileName) throws Exception {
		Properties props = new Properties();

		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		final MailMergeMetadata mailMergeMetadata = MailMergeMetadata
				.of(inputStream);

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(mailMergeMetadata
								.getEmail(), mailMergeMetadata.getPassword());
					}
				});

		MimeBodyPart attachmentBodyPart = getAttchment(resumeBAIS, fileName);

		for (Contact aContact : contacts) {
			sendEmail(session, aContact, mailMergeMetadata, attachmentBodyPart);
		}

		System.out.println("Done");
	}

	private void sendEmail(Session session, Contact contact,
			MailMergeMetadata mailMergeMetadata,
			MimeBodyPart attachmentBodyPart) throws Exception {

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(mailMergeMetadata.getEmail()));
		message.setRecipients(Message.RecipientType.TO,
				InternetAddress.parse(contact.getEmail()));
		message.setSubject(mailMergeMetadata.getSubject());

		MimeBodyPart messageBodyPart = getBody(contact, mailMergeMetadata);

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(messageBodyPart);
		multipart.addBodyPart(attachmentBodyPart);

		// Put parts in message
		message.setContent(multipart);

		Transport.send(message);
	}

	private MimeBodyPart getBody(Contact contact,
			MailMergeMetadata mailMergeMetadata) throws URISyntaxException,
			IOException, MessagingException {

		String text = getContent(contact, mailMergeMetadata);

		// create the message part
		MimeBodyPart messageBodyPart = new MimeBodyPart();
		messageBodyPart.setText(text, "utf-8", "html");

		return messageBodyPart;
	}

	private MimeBodyPart getAttchment(ByteArrayInputStream resumeBAIS,
			String fileName) throws MessagingException, IOException {

		MimeBodyPart attachmentBodyPart = new MimeBodyPart();

		DataSource source = new ByteArrayDataSource(resumeBAIS,
				"application/vnd.openxmlformats-officedocument.wordprocessingml.document");

		attachmentBodyPart.setDataHandler(new DataHandler(source));
		attachmentBodyPart.setFileName(fileName);

		return attachmentBodyPart;
	}

	public String getContent(Contact contact,
			MailMergeMetadata mailMergeMetadata) throws URISyntaxException,
			IOException {
		StringBuilder content = new StringBuilder();

		content.append("Hi " + contact.getFirstName() + ", \n");
		content.append(mailMergeMetadata.getBody());

		return content.toString();
	}

}
